package com.real.operationalcontrols.rxhttp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Window;

import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public abstract class MyDialogObserver implements Observer<String> {
    private ProgressDialog dialog;

    public MyDialogObserver(Activity activity) {
        if (activity != null && dialog == null) {
            dialog = new ProgressDialog(activity);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCanceledOnTouchOutside(false);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.show();
        }
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(String result) {
        if (dialog != null || dialog.isShowing()) {
            dialog.dismiss();
        }
        onSuccess(result);
    }

    @Override
    public void onError(Throwable e) {
        if (dialog != null || dialog.isShowing()) {
            dialog.dismiss();
        }
        if (e.getMessage() != null) {
            ToastUtils.showShort(e.getMessage());
        }
        Log.i("error", "网络请求错误--" + e.getMessage());
    }

    @Override
    public void onComplete() {
        if (dialog != null || dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public abstract void onSuccess(String result);

}
